package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.AreaInfoQry;
import com.jftone.bh.app.model.*;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.ComponentException;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.Date;
import java.util.List;


@Service
public class BaseService {
	
	private Logger logger = Logger.getLogger(BaseService.class);
	
	@Autowired
	private Dao dao;

	@Autowired
	private OSSFileManagerService ossService;

	public List<IData<String, Object>> queryAreaBySuperCodeFroRedis(String superCode) throws ComponentException {
		List<IData<String, Object>> areaList = null;
		try{
			areaList  = AreaInfoQry.query(superCode);
		}catch(Exception e){
			logger.error("加载地区数据错误" + e.getMessage(), e);
			throw new ComponentException("加载地区数据错误" + e.getMessage(), e);
		}
		return areaList;
	}

	public List<IData<String, Object>> queryAreaBySuperCode(String superCode) throws ComponentException {
		List<IData<String, Object>> areaList = null;
		try{
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("SUPER_CODE", superCode);
			areaList  = dao.query("queryAreaBySuperCode", inData);
		}catch(DaoException e){
			logger.error("加载地区数据错误" + e.getMessage(), e);
			throw new ComponentException("加载地区数据错误" + e.getMessage(), e);
		}
		return areaList;
	}

	public List<IData<String, Object>> queryBaseList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> baseList = null;
		String bname = data.getString("bname", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(bname)){
			param.put("BNAME", "%" + bname + "%");
		}
		baseList = dao.query("queryBaseList", param, page);
		for (IData<String, Object> base:baseList) {
			base.put("ADDRESS", AreaInfoQry.getAreaName(base.getString("SHENG","0"),base.getString("SHI","0"),base.getString("XIAN","0")));
		}
		return baseList;
	}

	@Transactional
	public int saveBase(IData<String, Object> data, boolean updateFlag) throws DaoException {
		NtbBase base = new NtbBase();
		base.setAbout(data.getString("about",""));
		base.setBname(data.getString("bname",""));
		base.setBusinessCircle(data.getString("businessCircle",""));
		base.setTel(data.getString("tel",""));
		base.setSheng(data.getInt("sheng",0));
		base.setShi(data.getInt("shi",0));
		base.setXian(data.getInt("xian",0));
		base.setAddr(data.getString("addr",""));
		base.setMustAmount(data.getInt("mustAmount",0));
		base.setLongitude(data.getDouble("longitude",0D));
		base.setLatitude(data.getDouble("latitude",0D));
		base.setBaseMali((int)(data.getDouble("baseMali",0D)*100));
		base.setVipMali((int)(data.getDouble("vipMali",0D)*100));
		base.setMaxVip(data.getInt("maxVip",0));
		base.setGrade(data.getInt("grade",0));
		if(base.getGrade()>10 || base.getGrade()<0){
			return 1;
		}
		if(base.getBaseMali()<0 || base.getVipMali()<0){
			return 1;
		}


		base.setBusinessTime(data.getString("businessTime",""));
		base.setPic(data.getString("pic"));
		String imgId = data.getString("imgId", "");
		String oldImgSrc = data.getString("oldImgSrc", "");
		IData<String, Object> updateData = new DataMap<String, Object>();
		IData<String,Object> whereData = new DataMap<String,Object>();
		if(updateFlag){
			whereData.put("uplevel",SysConst.VIP_SUB);
			whereData.put("affiliation",data.getInt("bid"));
			int havaVip = (int)dao.count(NtbUser.class,whereData);
			if(base.getMaxVip()<havaVip){
				return 1;
			}
			base.setResidueVip(base.getMaxVip() - havaVip);
			base.setId(data.getInt("bid"));
			dao.update(base);
			whereData.clear();
			if(!data.getString("pic").equals(oldImgSrc)&& !StringUtil.isEmpty(oldImgSrc)) {
				updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_YES);
				whereData.put("imgSrc", oldImgSrc);
				dao.update(TmpImg.class, updateData, whereData);
				ossService.delete(oldImgSrc);
			}
		}else{
			base.setAddtime(DateUtil.format(new Date(),DateUtil.FMT_DATETIME));
			dao.save(base);
		}
		if(!StringUtil.isBlank(imgId) && StringUtil.isNumber(imgId)) {
			whereData.clear();
			updateData.clear();
			updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_USE);
			whereData.put("id", Long.parseLong(imgId));
			dao.update(TmpImg.class, updateData, whereData);
		}
		return 0;
	}

	public int delete(int baseId) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id", baseId);
		NtbBase base= dao.get(NtbBase.class, param);
		if(base == null){
			return 1;
		}
		if(!StringUtil.isBlank(base.getPic())){
			param.clear();
			param.put("imgSrc", base.getPic());
			dao.delete(TmpImg.class, param);
			ossService.delete(base.getPic());
		}
		dao.delete(base);
		return 0;
	}

	public NtbBase getNtbBase(IData<String, Object> inData) throws DaoException {

		return dao.get(NtbBase.class,inData);
	}

	public List<NtbBaseCoach> queryCoachList(IData<String, Object> inData) throws DaoException {
		return dao.query(NtbBaseCoach.class,inData);
	}

	public NtbBaseCoach getNtbBaseCoach(IData<String, Object> inData) throws DaoException {
		return dao.get(NtbBaseCoach.class,inData);
	}

	public int saveCoach(IData<String, Object> data, boolean updateFlag) throws DaoException {
		NtbBaseCoach baseCoach = new NtbBaseCoach();
		baseCoach.setName(data.getString("name",""));
		baseCoach.setMali((int)(data.getDouble("mali",0D)*100));
		baseCoach.setMobile(data.getString("mobile",""));
		baseCoach.setRank("1");
		baseCoach.setSex(data.getShort("sex", (short) 0));
		if(baseCoach.getMali()<0){
			return 1;
		}
		if(updateFlag){
			baseCoach.setId(data.getInt("coachId"));
			dao.update(baseCoach);
		}else{
			baseCoach.setBaseId(data.getInt("baseId"));
			dao.save(baseCoach);
		}
		return 0;
	}


	public int deleteCoach(int coachId) throws DaoException {
		NtbBaseCoach baseCoach = new NtbBaseCoach();
		baseCoach.setId(coachId);
		dao.delete(baseCoach);
		return 0;
	}
}
